/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gac.gacmod.propertysheet;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.util.Enumeration;
import org.gac.gacmod.model.Configuration;
import org.gac.gacmod.model.Step;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.PropertySupport;
import org.openide.nodes.Sheet;
import org.openide.nodes.Sheet.Set;
import org.openide.util.Exceptions;
import org.openide.util.Utilities;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author Tomek
 */
public class StepNode extends AbstractNode//BeanNode<Step>
{

    private Step _step;

    public StepNode(Step step)
    {
        super(new StepChildren(),Lookups.singleton(step));
        setDisplayName(step.getName() + " properties");
        _step = step;
    }


    @Override
    protected Sheet createSheet()
    {
//        Sheet sheet = Sheet.createDefault();
//        Sheet.Set set = Sheet.createPropertiesSet();
//        Step obj = getLookup().lookup(Step.class);
//
//        BeanInfo bi = Introspector.getBeanInfo(obj);
//        for( PropertyDescriptor pd: bi.getPropertyDescriptors())
//        {
//            Property p = n
//        }
//        sheet.


        /// the below almost works!!!
        Sheet result = Sheet.createDefault();
        Set superSheet = Sheet.createPropertiesSet();
        result.put(superSheet);
        try
        {
            BeanInfo inf = Utilities.getBeanInfo( _step.getClass());
            for( PropertyDescriptor  prop: inf.getPropertyDescriptors())
            {
                if( prop.getReadMethod() != null && prop.getReadMethod().getAnnotation(Configuration.class) != null )
                {

                PropertySupport.Reflection support = new PropertySupport.Reflection (
                                                         _step, prop.getPropertyType (),
                                                         prop.getReadMethod (), prop.getWriteMethod ()
                                                     );
                support.setName (prop.getName ());
                support.setDisplayName (prop.getDisplayName ());
                support.setShortDescription (prop.getShortDescription ());
                support.setPropertyEditorClass (prop.getPropertyEditorClass ());

                for (Enumeration e = prop.attributeNames(); e.hasMoreElements();) {
                    String aname = (String)e.nextElement();
                    support.setValue(aname, prop.getValue(aname));

                }
                superSheet.put(support);

//                    superSheet.put(prop.)
//                    if( superSheet.get(Sheet.PROPERTIES).get(prop.getName()) != null)
//                        superSheet.get(Sheet.PROPERTIES).remove(prop.getName());
                }
            }
        } catch (IntrospectionException ex)
        {
            Exceptions.printStackTrace(ex);
        }

        return result;
    }





//    @Override
//    protected Sheet createSheet()
//    {
//        Sheet sheet = Sheet.createDefault();
//        Sheet.Set set = Sheet.createPropertiesSet();
//        Step obj = getLookup().lookup(Step.class);
//
//        for (Method m : obj.getClass().getMethods())
//        {
//
//            if( !m.getName().startsWith("get"))
//                continue;
//
//            if (m.isAnnotationPresent(Configuration.class))
//            {
//                Method setter = null;
//                for (Method m2 : obj.getClass().getMethods())
//                {
//                    if(( m.getName().compareTo( m2.getName())!=0) && (m2.getName().substring(3).compareTo(m.getName().substring(3)) == 0 ))
//                    {
//                        setter = m2;
//                        break;
//                    }
//                }
//
//                Property<String> prop = new PropertySupport.Reflection<String>(obj,
//                        String.class, m, setter);
//
//
//
//                prop.setName(m.getName().substring(3));
//
//                set.put(prop);
//
//
//            }
//
//        }
//
//        sheet.put(set);
//        return sheet;
//
//    }
}
